﻿using ERPSystem.Common.Domains.PurchaseInvoiceCommon;
using ERPSystem.Common.Domains.supplier;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using MediatR;
using MySqlX.XDevAPI.Common;
using NPOI.Util;
using SqlSugar;
using System.Reflection.Metadata;

namespace SalesManagementCommon.API.SalesManagementCommons.PurchaseReturnOrderRead
{
    public class PurchaseReturnOrderBackFillHandler : IRequestHandler<PurchaseReturnOrderBackFillCommand, Result<PurchaseReturnOrderBackFillDto>>
    {
        private readonly IBaseRepository<PurchaseReturnOrder> _purchaseReturnOrderRepository;
        private readonly IBaseRepository<PurchaseReturnOrderSubtable> _purchaseReturnOrderSubtableRepository;
        private readonly IBaseRepository<PurchasedGoods> _purchaseReturnOrderGoodsRepository;

        public PurchaseReturnOrderBackFillHandler(IBaseRepository<PurchaseReturnOrder> purchaseReturnOrderRepository, IBaseRepository<PurchaseReturnOrderSubtable> purchaseReturnOrderSubtableRepository, IBaseRepository<PurchasedGoods> purchaseReturnOrderGoodsRepository)
        {
            _purchaseReturnOrderRepository = purchaseReturnOrderRepository;
            _purchaseReturnOrderSubtableRepository = purchaseReturnOrderSubtableRepository;
            _purchaseReturnOrderGoodsRepository = purchaseReturnOrderGoodsRepository;
        }


        /// <summary>
        /// 查看采购退货单数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PurchaseReturnOrderBackFillDto>> Handle(PurchaseReturnOrderBackFillCommand request, CancellationToken cancellationToken)
        {

            var result = new Result<PurchaseReturnOrderBackFillDto>();
            try
            {
                var purchaseReturnOrder = await (await _purchaseReturnOrderRepository.GetAll()).ToListAsync();
                var purchaseReturnOrderSubtable = await (await _purchaseReturnOrderSubtableRepository.GetAll()).ToListAsync();
                var purchaseReturnOrderGoods= await (await _purchaseReturnOrderGoodsRepository.GetAll()).ToListAsync();
                var list = (from a in purchaseReturnOrder
                            where a.PurchaseReturnOrderNo == request.PurchaseReturnOrderNo
                            select new PurchaseReturnOrderBackFillDto()
                            {
                                PurchaseReturnOrderId = a.PurchaseReturnOrderId,
                                PurchaseReturnOrderNo = a.PurchaseReturnOrderNo,
                                PurchaseReturnOrderSerialNumber =a.PurchaseReturnOrderSerialNumber,
                                BusinessType=a.BusinessType,
                                PurchaseOrderNo = a.PurchaseOrderNo,
                                DescInfo = a.DescInfo,
                                DocumentDate = a.DocumentDate,
                                Operator = a.Operator,
                                Supplier = a.Supplier,
                                DocumentsStatus = a.DocumentsStatus,
                                Audit = a.Audit,
                                ReturnsWarehouse = a.ReturnsWarehouse,
                                BusinessStatus = a.BusinessStatus,
                                AuditDate = a.AuditDate,
                                purchaseReturnOrderSubtableBackFillDtos = new List<PurchaseReturnOrderSubtableBackFillDto>()
                            }).FirstOrDefault();
                if (list != null)
                {
                    list.purchaseReturnOrderSubtableBackFillDtos = (from a in purchaseReturnOrderSubtable
                                                                    //join b in purchaseReturnOrderGoods on a.MaterialNo equals b.MaterialCode
                                                                    where a.PurchaseReturnOrderSerialNumber == list.PurchaseReturnOrderSerialNumber
                                                                    select new PurchaseReturnOrderSubtableBackFillDto()
                                                                    {
                                                                        PurchaseReturnOrderSubtableId = a.PurchaseReturnOrderSubtableId,
                                                                        CopyNo = a.CopyNo,
                                                                        MaterialNo = a.MaterialNo,
                                                                        MaterialName = a.MaterialName,
                                                                        LotNumber = a.LotNumber,
                                                                        Num = a.Num,
                                                                        OutboundShipmentsNum = a.OutboundShipmentsNum,
                                                                        ReturnInstructions = a.ReturnInstructions,
                                                                        PurchaseReturnOrderSerialNumber = a.PurchaseReturnOrderSerialNumber,
                                                                        //TypeSpecification =b.TypeSpecification,
                                                                        //DocumentType =b.DocumentType,
                                                                    }).ToList();
                }
                result.Data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
